﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sinacor.Infra.Core.ProcessManager.Server;
using PSE.ProcessManager.Server.InteligenceModules;
using PSE.ProcessManager.Common.Gateway;
using PSE.ProcessManager.Common.Process;

namespace Sinacor.Process.Bovespa.Distribuicao.Server.ProcessImplementation
{
    public class Importacao : SinacorBaseProcessServer<ProcessLogic.Importacao>, ISimpleExecutionProcess
    {
        #region Construtor

        /// <summary>
        /// Construtor padrão
        /// </summary>
        public Importacao()
        {
            this.Id = ProcessEntity.Catalog.Importacao.ProcessID;
            this.Name = "Importacao";

            this.ProcessLogic.ImportacaoIniciada += new EventHandler(EnviarMensagemImportacaoIniciada);
            this.ProcessLogic.ImportacaoConcluida += new EventHandler<ProcessEventArgs<ProcessEntity.Parameter.FiltroRetornoImportacaoDistribuicao>>(EnviarMensagemImportacaoConcluida);
        }


        #endregion

        #region Event Handlers

        private void EnviarMensagemImportacaoIniciada(object sender, EventArgs e)
        {
            this.SendMessage(new Message()
            {
                ProcessId = this.Id,
                MessageId = ProcessEntity.Catalog.Importacao.ImportacaoIniciada.MessageID
            });
        }

        private void EnviarMensagemImportacaoConcluida(object sender, ProcessEventArgs<ProcessEntity.Parameter.FiltroRetornoImportacaoDistribuicao> e)
        {
            this.SendMessage<ProcessEntity.Parameter.FiltroRetornoImportacaoDistribuicao>(
                                            new Message<ProcessEntity.Parameter.FiltroRetornoImportacaoDistribuicao>()
                                            {
                                                Parameter = e.Parameter,
                                                ProcessId = this.Id,
                                                MessageId = ProcessEntity.Catalog.Importacao.ImportacaoConcluida.MessageID
                                            });
        }


        #endregion

        #region Métodos

        /// 
        /// <param name="message"></param>
        public void ImportarDistribuicao(BaseMessage message)
        {
            bool fail = false;

            try
            {
                //while (!System.Diagnostics.Debugger.IsAttached)
                //    System.Threading.Thread.Sleep(1000);

                if (!IsAborted)
                {
                    this.ProcessLogic.ImportarDistribuicao(message.RetrieveParameter<ProcessEntity.Parameter.FiltroImportacaoDistribuicao>());
                }
            }
            catch (Exception ex)
            {
                fail = true;
                HandleException(ex);
            }
            finally
            {
                if(!this.ProcessLogic.ErroLeituraArquivo)
                    Finalize(fail);
            }
        }

        #endregion

        #region ISimpleExecutionProcess Members

        public void Execute(BaseMessage message)
        {
            this.Finalize();
        }

        #endregion
    }
}
